package com.fds.system.config;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * spring security 主配置类
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private SecurityProperties securityProperties;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 禁用 csrf 拦截
        http.csrf().disable()
                .sessionManagement()
                // 关闭session管理，使用token机制处理
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                // 未登录返回 JSON 格式的数据
                .httpBasic().authenticationEntryPoint((httpServletRequest, httpServletResponse, e) -> {
                    httpServletRequest.setCharacterEncoding("utf-8");
                    httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
                    httpServletResponse.getWriter().write(JSON.toJSONString("未登录"));
                })
                .and()
                // 无权访问 JSON 格式的数据
                .exceptionHandling().accessDeniedHandler((httpServletRequest, httpServletResponse, e) -> {
                    httpServletResponse.setCharacterEncoding("utf-8");
                    httpServletResponse.setHeader("Content-type", "application/json;charset=UTF-8");
                    httpServletResponse.getWriter().write(JSON.toJSONString("没有权限"));
                })
                .and()
                .authorizeRequests()
                // 对option不校验
                .antMatchers(HttpMethod.OPTIONS).permitAll()
                // 设置不校验白名单，走过滤器但是直接不拦截
                .antMatchers(securityProperties.getIgnoreUrl()).permitAll()
                .anyRequest().authenticated()
                .and()
                // 添加自定义请求jwt过滤器
                .addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
    }

    @Override
    public void configure(WebSecurity web) {
        // 设置拦截忽略文件夹，可以对静态资源放行 直接不走过滤器
        web.ignoring().antMatchers("/images/**");
    }
}
